using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CrazyDogToDo.Entity.Data;

namespace CrazyDogToDo.Service.Services
{
    public class MatterService
    {
        string myDayName = "我的一天";
        string myActivityName = "我的活动";
        private readonly CrazyDogToDoContext _crazy = new CrazyDogToDoContext();
        /// <summary>
        /// 创建事项
        /// </summary>
        /// <param name="email"></param>
        /// <param name="content"></param>
        public bool CreateMatter(string email,string content,string remark,string overdueTime)
        {
            var user = _crazy.User.SingleOrDefault(n => n.Email.Equals(email));
            var list = _crazy.Inventory.Where(y=>y.Email.Equals(email))
                .Where(m=>m.Title.Equals(myDayName))
                .SingleOrDefault(l => l.CreateTime.Day == DateTime.Now.Day);
            if(user == null || list == null)
            {
                return false;
            }
            else
            {
                var matter = new Matter()
                {
                    InventoryId = list.Id,
                    Email = email,
                    Content = content,
                    Remark = remark,
                    CreateTime = DateTime.Now,
                    OverdueTime = DateTime.Parse(overdueTime),
                    State = false,
                };
                _crazy.Matter.Add(matter);
                _crazy.SaveChanges();
                return true;
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        public void DeleteMatter(int id)
        {
            var matter = _crazy.Matter.SingleOrDefault(m => m.Id == id);
            _crazy.Matter.Remove(matter);
            _crazy.SaveChanges();
        }
        /// <summary>
        /// 显示“我的一天”清单下的所有事项
        /// </summary>
        /// <param name="inventoryId"></param>
        /// <returns></returns>
        public List<Matter> GetInventoryMatter(string email)
        {
            var inventory = _crazy.Inventory.Where(u => u.Email.Equals(email)).Where(n=>n.Title.Equals(myDayName)).SingleOrDefault(l => l.CreateTime.Day == DateTime.Now.Day);
            var matters = _crazy.Matter.Where(i => i.InventoryId == inventory.Id);
            if (inventory != null)
            {
                return matters.ToList();
            }
            else
                return null;
        }
        /// <summary>
        /// 显示"我的活动"清单下的所有事项
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public List<Matter> GetActivityMatter(string email)
        {
            var inventory = _crazy.Inventory.Where(u => u.Email.Equals(email)).SingleOrDefault(n => n.Title.Equals(myActivityName));
            var matters = _crazy.Matter.Where(i => i.InventoryId == inventory.Id);
            if (inventory != null)
            {
                return matters.ToList();
            }
            else
                return null;
        }
        /// <summary>
        /// 查看用户的所有事项
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public List<Matter> GetAllMatter(string email)
        {
            var matters = _crazy.Matter.Where(u => u.Email.Equals(email)).OrderBy(m => m.CreateTime);
            return matters.ToList();
        }
        /// <summary>
        /// 修改事项信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="newContent"></param>
        /// <param name="newRemark"></param>
        /// <param name="newOverdueTime"></param>
        /// <returns></returns>
        public bool ModifyMatter(int id, string newContent,string newRemark,string newOverdueTime)
        {
            var matter = _crazy.Matter.SingleOrDefault(m => m.Id == id);
            if (matter != null)
            {
                matter.Content = newContent;
                matter.Remark = newRemark;
                matter.OverdueTime = DateTime.Parse(newOverdueTime);
                _crazy.SaveChanges();
                return true;
            }
            else
                return false;
        }
        /// <summary>
        /// 改变事项的状态
        /// </summary>
        /// <param name="id"></param>
        public void ChangeMatterState(string email, int id)
        {
            /*完成某件事项取消完成某件事项*/
            var matter = _crazy.Matter.Where(u => u.Email.Equals(email)).SingleOrDefault(m => m.Id == id);
            matter.State = !matter.State;
            _crazy.SaveChanges();
        }
        /// <summary>
        /// 未完成事项提醒（所有）
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public List<Matter> UnfinishedReminders(string email)
        {
            /*找出所有以前未完成是事项按照时间顺序排列*/
            var matters = _crazy.Matter.Where(u => u.Email.Equals(email)).Where(s => s.State == false).OrderBy(m => m.OverdueTime);
            return matters.ToList();
        } 
        /// <summary>
        /// 根据Id找到某个事项
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Matter FindMatterById(int id)
        {
            var matter = _crazy.Matter.SingleOrDefault(u => u.Id == id);
            return matter;
        }
    }
}
